package com.album.activity.page;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.album.R;
import com.album.activity.page.PhotoImageListAdapter.ViewHolder; 
import com.album.common.Constants; 
import com.album.utils.MyLog;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ImageListAdapter extends CursorAdapter {
	private HashMap<String, SoftReference<Bitmap>> tempMap;
	private Bitmap bitmap;
	private BitmapFactory.Options options; 
    private final Context mContext;
    private LayoutInflater mInflater; 
    
	public ImageListAdapter(Context context, Cursor c) {
		super(context, c);
		mContext = context;
		tempMap = new HashMap<String, SoftReference<Bitmap>>(); 
		options = new BitmapFactory.Options();
		options.inSampleSize = 100;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setNotifyDataSetChanged(){
		notifyDataSetChanged();
	}
	 
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub 
		View view = mInflater.inflate(R.layout.image_item,null);
		ImageView imageView = (ImageView) view.findViewById(R.id.image);
		imageView.setLayoutParams(new RelativeLayout.LayoutParams(Constants.m_screenWidth,Constants.m_screenWidth));
		return view;
	}
	
	
	@Override
	public void bindView(View convertView, Context context, Cursor cursor) { 
		ViewHolder holder;
		ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
			String value = cursor.getString(cursor.getColumnIndex("_data"));
			try {
//				imageView.setImageBitmap(null);
//				if (tempMap != null && tempMap.containsKey(value) && tempMap.get(value).get() != null
//						&& !tempMap.get(value).get().isRecycled()) { 
//					imageView.setImageBitmap(tempMap.get(value).get());
//				} 
//				else {
					int id = cursor.getInt(cursor.getColumnIndex("_ID"));
//					new loadingImageFolder(imageView)	.execute(id);
//					MyLog.debug(getClass(), "........"+id);
					bitmap = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id,
							MediaStore.Images.Thumbnails.MICRO_KIND, options);
					Log.i("高和宽", bitmap.getHeight() + "," + bitmap.getWidth() + "");
//					tempMap.put(value, new SoftReference<Bitmap>(bitmap));
					imageView.setImageBitmap(bitmap);
//				}
			} catch (Exception e) { e.printStackTrace(); }
	}
	class loadingImageFolder extends AsyncTask<Integer,Bitmap,Integer>{
		
		private ImageView imageView;
		public loadingImageFolder(ImageView holder){
			this.imageView =holder;
		}
		@Override
		protected Integer doInBackground(Integer... params) {
	        	int id =  params[0];
	        	bitmap = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), id,
						MediaStore.Images.Thumbnails.MICRO_KIND, options);
	        	if(bitmap!=null){
	        		this.publishProgress(bitmap);
	        	}
			return 1;
		} 
		
		@Override
		protected void onProgressUpdate(Bitmap... bitmap) {
			// TODO Auto-generated method stub 
			if(bitmap!=null){
				MyLog.debug(getClass(), "---------------");
				imageView.setImageBitmap(bitmap[0]);
//        		notifyDataSetChanged();
			}
		}
		@Override  
	    protected void onCancelled() {  
			super.onCancelled();  
	    }  
		 
		@Override
		protected void onPostExecute(Integer result) {  
    		notifyDataSetChanged();
		}
	}
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public void clearDataSource(){
    	try {
    		if(tempMap!=null && tempMap.size()>0){
        		Iterator<Entry<String, SoftReference<Bitmap>>> it = tempMap.entrySet().iterator();
        		while(it.hasNext()){
        			Map.Entry<String, SoftReference<Bitmap>> entry = (Map.Entry<String, SoftReference<Bitmap>>) it.next();
        			SoftReference<Bitmap> sBitmap = entry.getValue();
        			Bitmap bitmap = sBitmap.get();
        			if(bitmap!=null && bitmap.isRecycled()){
        				bitmap.recycle(); 
        				bitmap=null;
        			}
        			MyLog.debug(getClass(), "........");
        			sBitmap.clear();
        			sBitmap=null;
        			entry=null;
        		}
        		tempMap.clear();
        		
        	}
		} catch (Exception e) { e.printStackTrace();}
    }
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return super.getCount();
	}
	
	public class ViewHolder {
		
		private ImageView imageView; 
	}
	
}
